#include <bits/stdc++.h>
#define MAXN 100001
using namespace std;
int n, m;
int ipt[MAXN];
int f[MAXN][20];
int lg2[MAXN];
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
void init() {
    for (int i = 0; i < n; i++) {
        f[i][0] = ipt[i];
    }
    for (int i = 2; i <= n; i++) {
        lg2[i] = lg2[i >> 1] + 1;
    }
    for (int j = 1; j <= lg2[n]; j++) {
        int step = 1 << j;
        for (int i = 0; i + step <= n; i++) {
            int mid = i + (1 << (j - 1));
            f[i][j] = max(f[i][j - 1], f[mid][j - 1]);
        }
    }
}

int main() {
    // freopen("3865test.in", "r", stdin);
    m = read(),n = read();
    ios::sync_with_stdio(false);
    for (int i = 0; i < n; i++) {
        ipt[i] = read();
    }
    init();
    while (m--) {
        int l, r;
        l = read(),r=read();
        l--; r--; // 将输入的1-based转为0-based索引
        int len = r - l + 1;
        int k = lg2[len];
        int ans = max(f[l][k], f[r - (1 << k) + 1][k]);
        cout << ans << '\n';
    }
    return 0;
}
